package persistency.controller;

import java.util.ArrayList;
import java.util.Collection;

import model.Business;
import model.BusinessTypeEnum;
import model.Course;
import model.CourseHistory;
import persistency.ArgIO;

public class CourseController extends BusinessController {

	private StringBuilder callableStatement;
	private static CourseController instance = new CourseController();
	private CourseController(){}
	
	public static CourseController instance(){
		return instance;
	}

	public boolean insertCourse(Course course) {
		callableStatement = new StringBuilder("{call sp_insertcourse(?,?,?,?)}");
		return insert(course,callableStatement,4);
	}
	
	public Course getCourse(Object id){
		return (Course)getBusinessObject(id,new StringBuilder("{call sp_getbyidcourse(?)}"),BusinessTypeEnum.COURSE);
	}
	
	public Collection<Course> getByCourseIDRequiredCourses(int id) {
		Collection<Course> col = new ArrayList<Course>();
		col.addAll((Collection<? extends Course>) getBusinessObjects(id,new StringBuilder("{call sp_getbycourseidrequiredcourses(?)}"),BusinessTypeEnum.COURSE,java.sql.Types.INTEGER));
		return col;
	}
	

	public Collection<Course> getByTeacherIDCoursePreferences(String id) {
		Collection<Course> col = new ArrayList<Course>();
		col.addAll((Collection<? extends Course>) getBusinessObjects(id,new StringBuilder("{call sp_getbyidteachercoursepreference(?)}"),BusinessTypeEnum.COURSE,java.sql.Types.CHAR));
		return col;
	}
	
	public Collection<Course> getAll() {
		Collection<Course> col = new ArrayList<Course>();
		col.addAll((Collection<? extends Course>) getBusinessObjects(new StringBuilder("{call sp_getallcourse}"),BusinessTypeEnum.COURSE));
		return col;
	}
	
	

	@Override
	public void prepare(Business business, Object[] args, int[] argsType, ArgIO[] argsIO, int i) throws Exception {
		Course course = (Course) business;
		switch (i) {

		/*
		 * in coursecode integer, 
		 * in name varchar(25),
		 * in minutes integer, 
		 * in credits integer
		 */

		case 0: {
			args[i] = course.getCode();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 1: {
			args[i] = course.getName();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 2: {
			args[i] = course.getCourseMinutes();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 3: {
			args[i] = course.getCredits();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}

		default:
			throw new Exception("Error while creating SP createCourse");
		}
		
	}

	
	

}
